package com.java;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import static com.java.BigCalculatorUtils.*;
/**
 * 计算工具类
 *
 * @author chen52
 *
 */
public class CalculatorUtils {

    /**
     * 判断是否是数字
     *
     * @param number
     * @return
     */
    public boolean isNumber(String number) {
        boolean flag = false;
        try {
            flag = numberValid(number);
        } catch (Exception e) {
            return flag;
        }
        return flag;
    }

    /**
     * 进行加减乘除计算
     *
     * @param numbers
     * @param operator
     * @throws Exception
     */
    void calculate(Stack<String> numbers, String operator) throws ExpressionFormatException {
        // 按照从左向右计算，则先弹出栈顶的数据是：被除数。
        String num2 = numbers.pop();
        String num1 = numbers.pop();

        switch (operator) {
            case "+":
                numbers.push ( add(num1 , num2) );
                break;
            case "-":
                numbers.push ( subtract(num1 , num2) );
                break;
            case "*":
                numbers.push ( multiply(num1 , num2) );
                break;
            case "/":
                if (num2.equals("0")) {
//                    System.out.println("--- 被除数不能为0 --- ");
                    throw new ExpressionFormatException("被除数不能为0");
                }
                numbers.push ( divide(num1 , num2) );
                break;
            default:
//                System.out.println("--- 表达式有误 --- ");
                throw new ExpressionFormatException("表达式有误");
        }
    }

    /**
     * 开平方
     *
     * @param numbers
     * @param operator
     * @throws Exception
     */
    void sqrt(Stack<String> numbers, String operator) throws ExpressionFormatException {
        //类型转换
        double dNumber =  Double.valueOf ( numbers.pop() ) ;
        if (dNumber < 0) {
//            System.out.println("--- 负数不能开平方 --- ");
            throw new ExpressionFormatException("负数不能开平方");
        }
        String sqrtNum = "" + Math.sqrt( dNumber ) ;
        numbers.push(sqrtNum);
    }



    /**
     * 撤销
     *
     * @param numbers
     * @param logList
     * @param operator
     * @throws Exception
     */
    void undo(Stack<String> numbers, Stack<List<String>> logList, String operator) throws ExpressionFormatException {

        // 将栈内数据清空
        while (!numbers.isEmpty()) {
            numbers.pop();
        }

        // 将上一步的操作数据存入操作数栈中
        if (!logList.isEmpty()) {
            logList.pop();// 弹出计算结果的日志
            List<String> numbersLog = logList.peek();// 获取计算前的栈数据
            for (String d : numbersLog) {
                if (d != null) {
                    numbers.push(d);
                }
            }
        }
    }

    /**
     * 清除
     *
     * @param numbers
     * @param logList
     * @param operator
     * @throws Exception
     */
    void clear(Stack<String> numbers, Stack<List<String>> logList, String operator) throws ExpressionFormatException {
        // 清理栈里的数据
        while (!numbers.isEmpty()) {
            numbers.pop();
        }
        // 清理动作在日志栈里存入null，用于回退时区分
        List<String> list = new ArrayList<>();
        list.add(null);
        logList.push(list);
    }
}